#include "StructList.h"
#include <memory.h>
PNODE createLinkList(void)
{
//创建了一个不存放有效数据的头结点
    PNODE pHead = (PNODE)malloc(sizeof(NODE));
    if(NULL == pHead)
    {
        printf("内存分配失败，程序退出！\n");
        exit(-1);
    }
    PNODE pTail = pHead; //pTail始终指向尾结点
    pTail->data.userid = -1;
    pTail->pNext = NULL; //清空指针域
    /*printf("请输入您想要创建链表结点的个数：len = ");
    scanf("%d", &length);
    for(i=0; i<length; i++)
    {
        printf("请输入第%d个结点的值：", i+1);
        scanf("%d", &value);
        PNODE pNew = (PNODE)malloc(sizeof(NODE));
        if(NULL == pHead)
        {
            printf("内存分配失败，程序退出！\n");
            exit(-1);
        }
        pNew->data = value;  //向新结点中放入值
        pTail->pNext = pNew; //将尾结点指向新结点
        pNew->pNext = NULL;  //将新结点的指针域清空
        pTail = pNew;   //将新结点赋给pTail,使pTail始终指向为尾结点
    }
    */
    return pHead;
}

void traverseLinkList(PNODE pHead)
{
    PNODE p = pHead->pNext;
    while(NULL != p)
    {
        printf("%d\t%s\t%s\t%d\n", p->data.userid,p->data.username,inet_ntoa(p->data.sockinfo.sin_addr),ntohs(p->data.sockinfo.sin_port));
        p = p->pNext;
    }
    printf("\n");
    return;
}

int isEmpty(PNODE pHead)
{
    if(NULL == pHead->pNext)
        return 1;
    else
        return 0;
}

int getLength(PNODE pHead)
{
    PNODE p = pHead->pNext;   //指向首节点
    int len = 0;     //记录链表长度的变量
    while(NULL != p)
    {
        len++;
        p = p->pNext;    //p指向下一结点
    }
    return len;
}

int insertElement(PNODE pHead, DATA* val)
{
    PNODE p = pHead;
    if(!val)
    {
       printf("插入失败\n");
        return 0;
    }
//判断p是否为空并且使p最终指向pos位置的结点
//创建一个新结点
    PNODE pNew = (PNODE)malloc(sizeof(NODE));
    if(NULL == pNew)
    {
        printf("内存分配失败,插入失败!\n");
       return 0;
    }
 //   pNew->data.sockinfo = val.sockinfo;
    memcpy(&(pNew->data),val,sizeof(DATA));
//定义一个临时结点，指向当前p的下一结点
    PNODE q = p->pNext;
//将p指向新结点
    p->pNext = pNew;
//将q指向之前p指向的结点
    pNew->pNext = q;
    return 1;
}

int deleteElement(PNODE pHead, int pos, DATA * pVal)
{
    int i = 0;
    PNODE p = pHead;
    PNODE q ;
//判断p是否为空并且使p最终指向pos结点
    while(NULL!=p->pNext && i<pos-1)
    {
        p = p->pNext;
        i++;
    }
    if(NULL==p->pNext || i>pos-1)
        return 0;
//保存要删除的结点
    * pVal = p->pNext->data;
//删除p后面的结点
    q = p->pNext;
    p->pNext = p->pNext->pNext;
    free(q);
    q = NULL;
    return 1;
}


DATA* serarchByUserId(PNODE pHead,unsigned int userid)
{
    PNODE p = pHead;
//判断p是否为空并且使p最终指向pos结点
    while(NULL!=p->pNext && p->pNext->data.userid!=userid)
    {
        p = p->pNext;
    }
      if(NULL==p->pNext )
      {
          printf("没有找到 userid:%d\n",userid);
          return NULL;
      }
      return &(p->pNext->data);
};
int deleteElementBaseUserId(PNODE pHead, unsigned int userid)
{
    PNODE p = pHead;
     PNODE q ;
//判断p是否为空并且使p最终指向pos结点
    while(NULL!=p->pNext && p->pNext->data.userid!=userid)
    {
        p = p->pNext;
    }
    if(NULL==p->pNext )
      {
          printf("没有找到 userid:%d\n",userid);
          return 0;
      }
//保存要删除的结点
 //   * pVal = p->pNext->data;
//删除p后面的结点
    q = p->pNext;
    p->pNext = p->pNext->pNext;
    free(q);
    q = NULL;
    return 1;

}
